package com.shambles.manage2.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.shambles.manage2.service.SysUserService;
import com.shambles.manage2.tkmapper.entity.AbattoirBooth;
import com.shambles.manage2.tkmapper.entity.MessageRequestVM;
import com.shambles.manage2.tkmapper.entity.RestResponse;
import com.shambles.manage2.tkmapper.entity.SysUser;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;

/**
 * 用户列表接口
 */
@RestController
@RequestMapping("sysUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户列表接口
     */
    @PostMapping("user")
    public RestResponse queryCost(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException {
        MessageRequestVM messageRequestVM=new MessageRequestVM();
        String post= jsonObject.getString("post");
        BeanUtils.populate(messageRequestVM,jsonObject);
        PageInfo<SysUser> pageInfo =  sysUserService.querUserAll(messageRequestVM,post);
        return RestResponse.ok(pageInfo);
    }

    /**
     * 删除商户
     */
    @PostMapping("deleteSysUser")
    RestResponse deleteSysUser(@RequestBody JSONObject jsonObject){
        String guid= jsonObject.getString("guid");
        this.sysUserService.deleteSysUser(guid);
        return RestResponse.ok();
    }

    /**
     * 修改商户
     */
    @PostMapping("updateSysUser")
    RestResponse updateSysUser(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException{
        SysUser sysUser = new SysUser();
        BeanUtils.populate(sysUser,jsonObject);
        this.sysUserService.updateSysUser(sysUser);
        return RestResponse.ok();
    }

    /**
     * 添加商户
     */
    @PostMapping("insertSysUser")
    RestResponse insertSysUser(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException{
        SysUser sysUser = new SysUser();
        BeanUtils.populate(sysUser,jsonObject);
        this.sysUserService.insertSysUser(sysUser);
        return RestResponse.ok();
    }

}
